package com.pvdnc.tapkit.chat.chat.client;

import android.util.Base64;

import com.pvdnc.nsocket.client.SimpleClientHandler;
import com.pvdnc.nsocket.protocol.PacketCodec;
import com.pvdnc.tapkit.chat.ChatService;
import com.pvdnc.tapkit.chat.MessageEntity;
import com.pvdnc.tapkit.chat.chat.ChatClient;
import com.pvdnc.tapkit.chat.chat.packet.TextMessage;

import org.w3c.dom.Text;

import java.nio.charset.Charset;

import io.netty.channel.ChannelHandlerContext;

public class OnMessageReceived extends SimpleClientHandler<ChatClient,TextMessage> {
    public OnMessageReceived(ChatClient client) {
        super(client, TextMessage.COMMAND);
    }

    @Override
    public void onReceived(ChannelHandlerContext ctx, TextMessage msg) {
        MessageEntity entity=new MessageEntity();
        entity.mName=msg.mSrcName;
        //解密消息内容
        byte[] encData= Base64.decode(msg.mContent,Base64.DEFAULT);
        byte[] data= getClient().mKeyManager.decrypt(encData);
        entity.mContent= PacketCodec.ENCODING.getString(data);

        entity.mLevel=msg.mLevel;
        MessageReceiver.broadcast(entity);
    }
}
